package com.ruoyi.controller;

/**
 * @author 26352
 * @Date 2023/2/27 19:57
 * @Description TODO
 * @Version 1.0
 */
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Query;
import com.ruoyi.entity.StudentEntity;
import com.ruoyi.mapper.MonMpaper;
import com.ruoyi.utils.Result;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 
 */

@Slf4j
@Validated
@RestController
@RequestMapping("demo")
public class MonController {
    /**
     * 注入一个mapper层对象
     */
    @Resource
    private MonMpaper monMpaper;

    @Resource
    MongoTemplate mongoTemplate;


    /**
     * 查询所有的方法
     */
    @RequestMapping ("findlist")
    public Result Monlist(){
        List<StudentEntity> Monlist = monMpaper.findAll();
        return new Result(1,"查询成功",Monlist);
    }

    /**
     * 添加方法
     */
    @RequestMapping("/add")
    public Result add(@RequestBody StudentEntity studentEntity){
        monMpaper.save(studentEntity);
        return new Result(1,"添加成功",null);
    }


    /**
     * 修改方法
     */
    @RequestMapping("/upd")
    public Result upd(@RequestBody StudentEntity studentEntity){
        monMpaper.save(studentEntity);
        return new Result(1,"修改成功",null);
    }

    /**
     * 删除方法
     */
    @RequestMapping("/del")
    public Result del(@RequestBody StudentEntity studentEntity){
        monMpaper.delete(studentEntity);
        return new Result(1,"删除成功",null);
    }



    //分页查询
    @RequestMapping("/findByPage")
    public List<StudentEntity> findByPage(Integer pageIndex, Integer pageSize) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(pageIndex, pageSize);
        query.with(pageable);
        List<StudentEntity> studentEntityList = mongoTemplate.find(query, StudentEntity.class);
        return studentEntityList;
    }


}
